<template>
  <div class="app-container">
    <div class="search-container">
      <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
        <el-form-item label="盘点月份" prop="checkYearMonth">
          <el-date-picker v-model="queryParams.checkYearMonth"
                          clearable
                          placeholder="请选择盘点月份"
                          type="date"
                          value-format="YYYY-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="盘点号" prop="inventoryNo">
          <el-input
            v-model="queryParams.inventoryNo"
            clearable
            placeholder="请输入盘点号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="保存标志(0,生成;1,暂存)" prop="type">
          <el-select v-model="queryParams.type" clearable placeholder="请选择保存标志(0,生成;1,暂存)">
            <el-option
              v-for="dict in dictDataMap['${dictType}']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="序号" prop="itemNo">
          <el-input
            v-model="queryParams.itemNo"
            clearable
            placeholder="请输入序号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="库存量主键" prop="stockId">
          <el-input
            v-model="queryParams.stockId"
            clearable
            placeholder="请输入库存量主键"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="消耗品编码" prop="materialCode">
          <el-input
            v-model="queryParams.materialCode"
            clearable
            placeholder="请输入消耗品编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="消耗品类别" prop="materialIndicator">
          <el-select v-model="queryParams.materialIndicator" clearable placeholder="请选择消耗品类别">
            <el-option
              v-for="dict in dictDataMap['material_style']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="materialInfoId" prop="infoId">
          <el-input
            v-model="queryParams.infoId"
            clearable
            placeholder="请输入materialInfoId"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="库存单位" prop="storageId">
          <el-input
            v-model="queryParams.storageId"
            clearable
            placeholder="请输入库存单位"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="包装数量" prop="packageNum">
          <el-input
            v-model="queryParams.packageNum"
            clearable
            placeholder="请输入包装数量"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="包装规格" prop="packageSpec">
          <el-input
            v-model="queryParams.packageSpec"
            clearable
            placeholder="请输入包装规格"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="包装单位" prop="packageUnits">
          <el-select v-model="queryParams.packageUnits" clearable placeholder="请选择包装单位">
            <el-option
              v-for="dict in dictDataMap['spec_unit']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input
            v-model="queryParams.purchasePrice"
            clearable
            placeholder="请输入进货价"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="批发价格" prop="tradePrice">
          <el-input
            v-model="queryParams.tradePrice"
            clearable
            placeholder="请输入批发价格"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="零售价格" prop="retailPrice">
          <el-input
            v-model="queryParams.retailPrice"
            clearable
            placeholder="请输入零售价格"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="批号" prop="batchNo">
          <el-input
            v-model="queryParams.batchNo"
            clearable
            placeholder="请输入批号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="药品追溯码集合" prop="trackCodes">
          <el-input
            v-model="queryParams.trackCodes"
            clearable
            placeholder="请输入药品追溯码集合"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="有效期" prop="expireDate">
          <el-date-picker v-model="queryParams.expireDate"
                          clearable
                          placeholder="请选择有效期"
                          type="date"
                          value-format="YYYY-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="帐面数量(小包装)" prop="accountQuantity">
          <el-input
            v-model="queryParams.accountQuantity"
            clearable
            placeholder="请输入帐面数量(小包装)"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="实盘数量(小包装)" prop="actualQuantity">
          <el-input
            v-model="queryParams.actualQuantity"
            clearable
            placeholder="请输入实盘数量(小包装)"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="大包装库存量" prop="maxQuantity">
          <el-input
            v-model="queryParams.maxQuantity"
            clearable
            placeholder="请输入大包装库存量"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="小包装库存量" prop="minQuantity">
          <el-input
            v-model="queryParams.minQuantity"
            clearable
            placeholder="请输入小包装库存量"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="最小单位" prop="minUnits">
          <el-select v-model="queryParams.minUnits" clearable placeholder="请选择最小单位">
            <el-option
              v-for="dict in dictDataMap['spec_unit']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="最小规格" prop="minSpec">
          <el-input
            v-model="queryParams.minSpec"
            clearable
            placeholder="请输入最小规格"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="账面额" prop="accountMoney">
          <el-input
            v-model="queryParams.accountMoney"
            clearable
            placeholder="请输入账面额"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="实盘额" prop="actualMoney">
          <el-input
            v-model="queryParams.actualMoney"
            clearable
            placeholder="请输入实盘额"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="账面额销售" prop="accountMoneyJj">
          <el-input
            v-model="queryParams.accountMoneyJj"
            clearable
            placeholder="请输入账面额销售"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="实盘额销售" prop="actualMoneyJj">
          <el-input
            v-model="queryParams.actualMoneyJj"
            clearable
            placeholder="请输入实盘额销售"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="盈亏额销售" prop="profitMoneyJj">
          <el-input
            v-model="queryParams.profitMoneyJj"
            clearable
            placeholder="请输入盈亏额销售"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="机构ID" prop="orgId">
          <el-input
            v-model="queryParams.orgId"
            clearable
            placeholder="请输入机构ID"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="机构名称" prop="orgName">
          <el-input
            v-model="queryParams.orgName"
            clearable
            placeholder="请输入机构名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建人名称" prop="createByName">
          <el-input
            v-model="queryParams.createByName"
            clearable
            placeholder="请输入创建人名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="更新人名称" prop="updateByName">
          <el-input
            v-model="queryParams.updateByName"
            clearable
            placeholder="请输入更新人名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="预留1" prop="reserve1">
          <el-input
            v-model="queryParams.reserve1"
            clearable
            placeholder="请输入预留1"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="预留2" prop="reserve2">
          <el-input
            v-model="queryParams.reserve2"
            clearable
            placeholder="请输入预留2"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="预留3" prop="reserve3">
          <el-input
            v-model="queryParams.reserve3"
            clearable
            placeholder="请输入预留3"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="预留4" prop="reserve4">
          <el-input
            v-model="queryParams.reserve4"
            clearable
            placeholder="请输入预留4"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="最小单位名称" prop="minSpecName">
          <el-input
            v-model="queryParams.minSpecName"
            clearable
            placeholder="请输入最小单位名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="包装单位名称" prop="packageUnitsName">
          <el-input
            v-model="queryParams.packageUnitsName"
            clearable
            placeholder="请输入包装单位名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['material:MaterialInventoryCheckPreStorage:add']"
          plain
          type="primary"
          @click="handleAdd"
        >
          <template
            #icon>
            <i-ep-plus/>
          </template>
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['material:MaterialInventoryCheckPreStorage:edit']"
          :disabled="single"
          plain
          type="success"
          @click="handleUpdate"
        >
          <template #icon>
            <i-ep-edit/>
          </template>
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['material:MaterialInventoryCheckPreStorage:remove']"
          :disabled="multiple"
          plain
          type="danger"
          @click="handleDelete"
        >
          <template #icon>
            <i-ep-delete/>
          </template>
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['material:MaterialInventoryCheckPreStorage:export']"
          plain
          type="warning"
          @click="handleExport"
        >
          <template #icon>
            <i-ep-download/>
          </template>
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :border="true" :data="MaterialInventoryCheckPreStorageList" :header-cell-style="$headerCellStyle"
              :stripe="true" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column :align="'center'" type="selection" width="55"/>
      <el-table-column v-if="true" :align="'center'" label="主键" prop="inventoryCheckPreStorageId"/>
      <el-table-column :align="'center'" label="盘点月份" prop="checkYearMonth" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkYearMonth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="盘点号" prop="inventoryNo"/>
      <el-table-column :align="'center'" label="保存标志(0,生成;1,暂存)" prop="type"/>
      <el-table-column :align="'center'" label="序号" prop="itemNo"/>
      <el-table-column :align="'center'" label="库存量主键" prop="stockId"/>
      <el-table-column :align="'center'" label="消耗品编码" prop="materialCode"/>
      <el-table-column :align="'center'" label="消耗品类别" prop="materialIndicator">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['material_style']" :value="scope.row.materialIndicator"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="materialInfoId" prop="infoId"/>
      <el-table-column :align="'center'" label="库存单位" prop="storageId"/>
      <el-table-column :align="'center'" label="包装数量" prop="packageNum"/>
      <el-table-column :align="'center'" label="包装规格" prop="packageSpec"/>
      <el-table-column :align="'center'" label="包装单位" prop="packageUnits">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="进货价" prop="purchasePrice"/>
      <el-table-column :align="'center'" label="批发价格" prop="tradePrice"/>
      <el-table-column :align="'center'" label="零售价格" prop="retailPrice"/>
      <el-table-column :align="'center'" label="批号" prop="batchNo"/>
      <el-table-column :align="'center'" label="药品追溯码集合" prop="trackCodes"/>
      <el-table-column :align="'center'" label="有效期" prop="expireDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="帐面数量(小包装)" prop="accountQuantity"/>
      <el-table-column :align="'center'" label="实盘数量(小包装)" prop="actualQuantity"/>
      <el-table-column :align="'center'" label="大包装库存量" prop="maxQuantity"/>
      <el-table-column :align="'center'" label="小包装库存量" prop="minQuantity"/>
      <el-table-column :align="'center'" label="最小单位" prop="minUnits">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="最小规格" prop="minSpec"/>
      <el-table-column :align="'center'" label="账面额" prop="accountMoney"/>
      <el-table-column :align="'center'" label="实盘额" prop="actualMoney"/>
      <el-table-column :align="'center'" label="账面额销售" prop="accountMoneyJj"/>
      <el-table-column :align="'center'" label="实盘额销售" prop="actualMoneyJj"/>
      <el-table-column :align="'center'" label="盈亏额销售" prop="profitMoneyJj"/>
      <el-table-column :align="'center'" label="机构ID" prop="orgId"/>
      <el-table-column :align="'center'" label="机构名称" prop="orgName"/>
      <el-table-column :align="'center'" label="创建人名称" prop="createByName"/>
      <el-table-column :align="'center'" label="更新人名称" prop="updateByName"/>
      <el-table-column :align="'center'" label="预留1" prop="reserve1"/>
      <el-table-column :align="'center'" label="预留2" prop="reserve2"/>
      <el-table-column :align="'center'" label="预留3" prop="reserve3"/>
      <el-table-column :align="'center'" label="预留4" prop="reserve4"/>
      <el-table-column :align="'center'" label="最小单位名称" prop="minSpecName"/>
      <el-table-column :align="'center'" label="包装单位名称" prop="packageUnitsName"/>
      <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作" width="220">
        <template #default="scope">
          <el-button
            v-hasPerm="['material:MaterialInventoryCheckPreStorage:edit']"
            type="success"
            @click="handleUpdate(scope.row)"
          >
            <template #icon>
              <i-ep-edit/>
            </template>
            修改
          </el-button>
          <el-button
            v-hasPerm="['material:MaterialInventoryCheckPreStorage:remove']"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            <template #icon>
              <i-ep-delete/>
            </template>
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改消耗品库存盘点对话框 -->
    <el-dialog v-model="open" :close-on-click-modal="false" :title="title" append-to-body width="500px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="盘点月份" prop="checkYearMonth">
          <el-date-picker v-model="form.checkYearMonth"
                          clearable
                          placeholder="请选择盘点月份"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="盘点号" prop="inventoryNo">
          <el-input v-model="form.inventoryNo" placeholder="请输入盘点号"/>
        </el-form-item>
        <el-form-item label="保存标志(0,生成;1,暂存)" prop="type">
          <el-select v-model="form.type" placeholder="请选择保存标志(0,生成;1,暂存)">
            <el-option
              v-for="dict in dictDataMap['${dictType}']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="序号" prop="itemNo">
          <el-input v-model="form.itemNo" placeholder="请输入序号"/>
        </el-form-item>
        <el-form-item label="库存量主键" prop="stockId">
          <el-input v-model="form.stockId" placeholder="请输入库存量主键"/>
        </el-form-item>
        <el-form-item label="消耗品编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入消耗品编码"/>
        </el-form-item>
        <el-form-item label="消耗品类别" prop="materialIndicator">
          <el-select v-model="form.materialIndicator" placeholder="请选择消耗品类别">
            <el-option
              v-for="dict in dictDataMap['material_style']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="materialInfoId" prop="infoId">
          <el-input v-model="form.infoId" placeholder="请输入materialInfoId"/>
        </el-form-item>
        <el-form-item label="库存单位" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入库存单位"/>
        </el-form-item>
        <el-form-item label="包装数量" prop="packageNum">
          <el-input v-model="form.packageNum" placeholder="请输入包装数量"/>
        </el-form-item>
        <el-form-item label="包装规格" prop="packageSpec">
          <el-input v-model="form.packageSpec" placeholder="请输入包装规格"/>
        </el-form-item>
        <el-form-item label="包装单位" prop="packageUnits">
          <el-select v-model="form.packageUnits" placeholder="请选择包装单位">
            <el-option
              v-for="dict in dictDataMap['spec_unit']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入进货价"/>
        </el-form-item>
        <el-form-item label="批发价格" prop="tradePrice">
          <el-input v-model="form.tradePrice" placeholder="请输入批发价格"/>
        </el-form-item>
        <el-form-item label="零售价格" prop="retailPrice">
          <el-input v-model="form.retailPrice" placeholder="请输入零售价格"/>
        </el-form-item>
        <el-form-item label="批号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批号"/>
        </el-form-item>
        <el-form-item label="药品追溯码集合" prop="trackCodes">
          <el-input v-model="form.trackCodes" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
        <el-form-item label="有效期" prop="expireDate">
          <el-date-picker v-model="form.expireDate"
                          clearable
                          placeholder="请选择有效期"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="帐面数量(小包装)" prop="accountQuantity">
          <el-input v-model="form.accountQuantity" placeholder="请输入帐面数量(小包装)"/>
        </el-form-item>
        <el-form-item label="实盘数量(小包装)" prop="actualQuantity">
          <el-input v-model="form.actualQuantity" placeholder="请输入实盘数量(小包装)"/>
        </el-form-item>
        <el-form-item label="大包装库存量" prop="maxQuantity">
          <el-input v-model="form.maxQuantity" placeholder="请输入大包装库存量"/>
        </el-form-item>
        <el-form-item label="小包装库存量" prop="minQuantity">
          <el-input v-model="form.minQuantity" placeholder="请输入小包装库存量"/>
        </el-form-item>
        <el-form-item label="最小单位" prop="minUnits">
          <el-select v-model="form.minUnits" placeholder="请选择最小单位">
            <el-option
              v-for="dict in dictDataMap['spec_unit']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最小规格" prop="minSpec">
          <el-input v-model="form.minSpec" placeholder="请输入最小规格"/>
        </el-form-item>
        <el-form-item label="账面额" prop="accountMoney">
          <el-input v-model="form.accountMoney" placeholder="请输入账面额"/>
        </el-form-item>
        <el-form-item label="实盘额" prop="actualMoney">
          <el-input v-model="form.actualMoney" placeholder="请输入实盘额"/>
        </el-form-item>
        <el-form-item label="账面额销售" prop="accountMoneyJj">
          <el-input v-model="form.accountMoneyJj" placeholder="请输入账面额销售"/>
        </el-form-item>
        <el-form-item label="实盘额销售" prop="actualMoneyJj">
          <el-input v-model="form.actualMoneyJj" placeholder="请输入实盘额销售"/>
        </el-form-item>
        <el-form-item label="盈亏额销售" prop="profitMoneyJj">
          <el-input v-model="form.profitMoneyJj" placeholder="请输入盈亏额销售"/>
        </el-form-item>
        <el-form-item label="机构ID" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入机构ID"/>
        </el-form-item>
        <el-form-item label="机构名称" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入机构名称"/>
        </el-form-item>
        <el-form-item label="创建人名称" prop="createByName">
          <el-input v-model="form.createByName" placeholder="请输入创建人名称"/>
        </el-form-item>
        <el-form-item label="更新人名称" prop="updateByName">
          <el-input v-model="form.updateByName" placeholder="请输入更新人名称"/>
        </el-form-item>
        <el-form-item label="预留1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留1"/>
        </el-form-item>
        <el-form-item label="预留2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留2"/>
        </el-form-item>
        <el-form-item label="预留3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留3"/>
        </el-form-item>
        <el-form-item label="预留4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留4"/>
        </el-form-item>
        <el-form-item label="最小单位名称" prop="minSpecName">
          <el-input v-model="form.minSpecName" placeholder="请输入最小单位名称"/>
        </el-form-item>
        <el-form-item label="包装单位名称" prop="packageUnitsName">
          <el-input v-model="form.packageUnitsName" placeholder="请输入包装单位名称"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
  name: "MaterialInventoryCheckPreStorage"
}
</script>

<script lang="ts" setup>
import {
  addMaterialInventoryCheckPreStorage,
  delMaterialInventoryCheckPreStorage,
  getMaterialInventoryCheckPreStorage,
  listMaterialInventoryCheckPreStorage,
  updateMaterialInventoryCheckPreStorage
} from "@/api/material/MaterialInventoryCheckPreStorage";

import {useDictStoreHook} from "@/store/modules/dict";

const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('spec_unit', 'material_style'))


const {proxy} = getCurrentInstance() as any;

const formRef = ref();
const queryParamsRef = ref()
const MaterialInventoryCheckPreStorageList = ref([]);
const open = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const form = ref<any>({
  checkYearMonth: "",
  inventoryNo: "",
  type: "",
  itemNo: 0,
  stockId: "",
  materialCode: "",
  materialIndicator: "",
  infoId: "",
  storageId: "",
  packageNum: 0,
  packageSpec: "",
  packageUnits: "",
  purchasePrice: 0,
  tradePrice: 0,
  retailPrice: 0,
  batchNo: "",
  trackCodes: "",
  expireDate: "",
  accountQuantity: 0,
  actualQuantity: 0,
  maxQuantity: 0,
  minQuantity: 0,
  minUnits: "",
  minSpec: "",
  accountMoney: 0,
  actualMoney: 0,
  accountMoneyJj: 0,
  actualMoneyJj: 0,
  profitMoneyJj: 0,
  orgId: "",
  orgName: "",
  createByName: "",
  updateByName: "",
  reserve1: "",
  reserve2: "",
  reserve3: "",
  reserve4: "",
  minSpecName: "",
  packageUnitsName: ""
})
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  checkYearMonth: "",
  inventoryNo: "",
  type: "",
  itemNo: 0,
  stockId: "",
  materialCode: "",
  materialIndicator: "",
  infoId: "",
  storageId: "",
  packageNum: 0,
  packageSpec: "",
  packageUnits: "",
  purchasePrice: 0,
  tradePrice: 0,
  retailPrice: 0,
  batchNo: "",
  trackCodes: "",
  expireDate: "",
  accountQuantity: 0,
  actualQuantity: 0,
  maxQuantity: 0,
  minQuantity: 0,
  minUnits: "",
  minSpec: "",
  accountMoney: 0,
  actualMoney: 0,
  accountMoneyJj: 0,
  actualMoneyJj: 0,
  profitMoneyJj: 0,
  orgId: "",
  orgName: "",
  createByName: "",
  updateByName: "",
  reserve1: "",
  reserve2: "",
  reserve3: "",
  reserve4: "",
  minSpecName: "",
  packageUnitsName: ""
})
const rules = ref<any>({
  inventoryCheckPreStorageId: [
    {
      required: true, message: "主键不能为空", trigger: "blur"
    }
  ],
  materialCode: [
    {
      required: true, message: "消耗品编码不能为空", trigger: "blur"
    }
  ],
  infoId: [
    {
      required: true, message: "materialInfoId不能为空", trigger: "blur"
    }
  ],
  storageId: [
    {
      required: true, message: "库存单位不能为空", trigger: "blur"
    }
  ],
  packageSpec: [
    {
      required: true, message: "包装规格不能为空", trigger: "blur"
    }
  ],
  packageUnits: [
    {
      required: true, message: "包装单位不能为空", trigger: "change"
    }
  ],
  batchNo: [
    {
      required: true, message: "批号不能为空", trigger: "blur"
    }
  ],
  orgId: [
    {
      required: true, message: "机构ID不能为空", trigger: "blur"
    }
  ],
  orgName: [
    {
      required: true, message: "机构名称不能为空", trigger: "blur"
    }
  ],
})


/** 查询消耗品库存盘点列表 */
function getList() {
  listMaterialInventoryCheckPreStorage(queryParams.value).then((response: any) => {
    MaterialInventoryCheckPreStorageList.value = response.rows;
    total.value = response.total;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    inventoryCheckPreStorageId: "",
    checkYearMonth:
      "",
    inventoryNo:
      "",
    type:
      "",
    stockId:
      "",
    materialCode:
      "",
    materialIndicator:
      "",
    infoId:
      "",
    storageId:
      "",
    packageSpec:
      "",
    packageUnits:
      "",
    batchNo:
      "",
    trackCodes:
      "",
    expireDate:
      "",
    minUnits:
      "",
    minSpec:
      "",
    orgId:
      "",
    orgName:
      "",
    delFlag:
      "",
    createBy:
      "",
    createByName:
      "",
    createTime:
      "",
    updateBy:
      "",
    updateByName:
      "",
    updateTime:
      "",
    reserve1:
      "",
    reserve2:
      "",
    reserve3:
      "",
    reserve4:
      "",
    minSpecName:
      "",
    packageUnitsName:
      ""
  }
  ;
  nextTick(() => {
    formRef.value.resetFields()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  // proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.inventoryCheckPreStorageId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  if (!queryParams.value.orgId) {
    return ElMessage.error("请先选择机构")
  }
  open.value = true;
  reset();
  title.value = "添加消耗品库存盘点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  reset();
  const _inventoryCheckPreStorageId = row.inventoryCheckPreStorageId || ids.value
  getMaterialInventoryCheckPreStorage(_inventoryCheckPreStorageId).then((response: any) => {
    form.value = response.data;
    title.value = "修改消耗品库存盘点";
  });
}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      if (form.value.inventoryCheckPreStorageId != null) {
        updateMaterialInventoryCheckPreStorage(form.value).then((response: any) => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      } else {
        addMaterialInventoryCheckPreStorage(form.value).then((response: any) => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _inventoryCheckPreStorageIds = row.inventoryCheckPreStorageId ||
    ids.value;
  ElMessageBox.confirm('是否确认删除消耗品库存盘点编号为"' + inventoryCheckPreStorageIds + '"的数据项？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delMaterialInventoryCheckPreStorage(_inventoryCheckPreStorageIds).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
  })
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('material/MaterialInventoryCheckPreStorage/export', {
    ...queryParams.value
  }, `MaterialInventoryCheckPreStorage_${new Date().getTime()}.xlsx`)
}

getList();
</script>
